package Dao;
import Dao.DatabaseConn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable {
   //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
  // static final String DB_URL = "jdbc:mysql://localhost/POS_db?serverTimezone=UTC";

   //static final String USER = "root";
   //static final String PASS = "root";
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      Class.forName("com.mysql.cj.jdbc.Driver");
      conn = DriverManager.getConnection(DatabaseConn.URL, DatabaseConn.USER, DatabaseConn.PASS);
      System.out.println("进入POS数据库");

      stmt = conn.createStatement();

      String sql =   /*前面四个表已经创建好了所以写成注释
        "CREATE TABLE if not exists  goods " +
                   "(name varchar(10), " +
                   " id integer(5), " + 
                   " sellingprice float(5), " + 
                   " costingprice float(5), " + 
                   " PRIMARY KEY ( id ))"; 
 "CREATE TABLE if not exists  staff " +
                   "(name varchar(5), " +
                   " id integer(5), " + 
                   " level varchar(5), " + 
                   " phonenumber varchar(5), " + 
                   " photo longblob, " + 
                   " PRIMARY KEY ( id ))"; 
"CREATE TABLE if not exists  sellorder " +
                   "(" +
                   " orderid varchar(10), " + 
                   " selldate datetime, " + 
                   " total float(6), " + 
                   " PRIMARY KEY ( orderid ))"; 
"CREATE TABLE if not exists  orderdetail " +
              "(" +
              " id int(5), " + 
              " orderid varchar(10), " +
              " sales int(5), " + 
              " price float(5), " + 
              " FOREIGN KEY (id) REFERENCES goods(id),"+
              " FOREIGN KEY (orderid) REFERENCES sellorder(orderid))";
      */
"CREATE TABLE if not exists  payment " +
              "(" +
              " payid varchar(11), " + 
              " money float(5), " +
              " orderid varchar(10), " + 
              " time datetime, " + 
              " method varchar(4), " + 
              " FOREIGN KEY (orderid) REFERENCES sellorder(orderid))";
    		  	   

      stmt.executeUpdate(sql);
      System.out.println("创建完成");
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
    	  
      }
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
  
   }
}